package com.boarsoft.boar.office.meeting.biz;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.boar.office.entity.MeetingInfo;
import com.boarsoft.boar.office.entity.MeetingUser;
import com.boarsoft.boar.office.entity.OfficeGroupUser;
import com.boarsoft.boar.office.meeting.MeetingBiz;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component("meetingBiz")
public class MeetingBizImpl extends SimpleBizImpl implements MeetingBiz {
	@Override
	@Transactional(readOnly = true)
	public PagedResult<MeetingInfo> list(String groupId, String userId, String key, String orderBy, int pageNo, int pageSize) {
		StringBuilder sb = new StringBuilder();
		sb.append(" from ").append(MeetingInfo.class.getName());
		sb.append(" o where o.status != ").append(MeetingInfo.STATUS_CLOSED);
		if (Util.strIsEmpty(groupId)) {
			sb.append(" and o.ownerId = '").append(userId).append("'");
		} else {
			sb.append(" and o.ownerId in (select u.userId from ");
			sb.append(OfficeGroupUser.class.getName());
			sb.append(" u where u.groupId = '").append(groupId).append("')");
		}
		if (Util.strIsNotEmpty(key)) {
			sb.append(" and (o.title like '%").append(key);
			sb.append("%') ");
		}
		//
		int total = dao.getTotal("select count(1)".concat(sb.toString()));
		if (Util.strIsEmpty(orderBy)) {
			sb.append(" order by o.ownerId, o.endTime, o.startTime desc");
		} else {
			sb.append(" order by ").append(orderBy);
		}
		//
		List<MeetingInfo> lt = dao.list(sb.toString(), MeetingInfo.class, pageNo, pageSize);
		return new PagedResult<MeetingInfo>(total, lt, pageNo, pageSize);
	}

	@Override
	@Transactional(readOnly = true)
	public MeetingInfo get(String id) {
		return dao.get(MeetingInfo.class, id);
	}

	@Override
	@Transactional
	public void save(MeetingInfo o, String[] userIds) {
		for (String uid : userIds) {
			MeetingUser a = new MeetingUser();
			a.setMeetingId(o.getId());
			a.setUserId(uid);
			dao.save(a);
		}
		dao.save(o);
	}

	@Override
	@Transactional
	public void save(MeetingInfo o) {
		dao.save(o);
	}

	@Override
	@Transactional
	public void update(MeetingInfo o, String[] userIds) {
		StringBuilder sb = new StringBuilder();
		sb.append("delete from ").append(MeetingUser.class.getName());
		sb.append(" where meetingId = '").append(o.getId()).append("'");
		dao.executeUpdate(sb.toString());
		for (String uid : userIds) {
			MeetingUser a = new MeetingUser();
			a.setMeetingId(o.getId());
			a.setUserId(uid);
			dao.save(a);
		}
		dao.merge(o);
	}

	@Override
	@Transactional
	public void update(MeetingInfo o) {
		dao.merge(o);
	}

	@Override
	@Transactional(readOnly = true)
	public PagedResult<MeetingInfo> list(String groupId, String ownerId, String key, Short status, String orderBy, int pageNo,
			int pageSize) {
		StringBuilder sb = new StringBuilder();
		sb.append(" from ").append(MeetingInfo.class.getName());
		sb.append(" where 1=1");
		if (status != null && status > 0) {
			sb.append(" and status = ").append(status);
		}
		if (Util.strIsEmpty(ownerId)) {
			if (Util.strIsEmpty(groupId)) {
				// TODO
			} else {
				sb.append(" and ownerId in (select userId from ").append(OfficeGroupUser.class.getName());
				sb.append(" where groupId = '").append(groupId).append("')");
			}
		} else {
			sb.append(" and ownerId = '").append(ownerId).append("'");
		}
		if (Util.strIsNotEmpty(key)) {
			sb.append(" and (title like '%").append(key);
			sb.append("%') ");
		}
		//
		int total = dao.getTotal("select count(1)".concat(sb.toString()));
		if (Util.strIsEmpty(orderBy)) {
			sb.append(" order by ownerId, endTime, startTime desc");
		} else {
			sb.append(" order by ").append(orderBy);
		}
		//
		List<MeetingInfo> lt = dao.list(sb.toString(), MeetingInfo.class, pageNo, pageSize);
		return new PagedResult<MeetingInfo>(total, lt, pageNo, pageSize);
	}
}